import PopupTreeInput from '@/components/PopupTreeInput'
import KtTable from '@/components/Core/KtTable'
import KtButton from '@/components/Core/KtButton'
import TableColumnFilterDialog from '@/components/Core/TableColumnFilterDialog'
import {mavonEditor} from 'mavon-editor'
import 'mavon-editor/dist/css/index.css'

export let data = {
  components: {
    PopupTreeInput,
    KtTable,
    KtButton,
    TableColumnFilterDialog,
    mavonEditor
  },
  data () {
    return {
      viewName: 'exam',
      filters: {
        name: '',
        authorName: ''
      },
      pageRequest: {pageNum: 1, pageSize: 15},
      pageResult: {},
      columns: [],
      filterColumns: [],
      size: 'small',
      exam: {
        type: 0,
        status: 1,
        examUnique: '',
        reason: '',
        examEdit: {
          title: '',
          htmlSubject: '',
          htmlContent: ''
        },
        types: [{
          value: 0,
          text: '文章'
        }, {
          value: 1,
          text: '题目'
        }, {
          value: 2,
          text: '经验交流'
        }, {
          value: 3,
          text: '解决方案'
        },{
          value: 4,
          text: '职位信息'
        }, {
          value: 5,
          text: '其他'
        }],
        statuses: [{
          value: null,
          text: ''
        }, {
          value: 1,
          text: '待审核'
        }, {
          value: 2,
          text: '待提交'
        }, {
          value: 3,
          text: '已删除'
        }, {
          value: 4,
          text: '隐藏'
        }, {
          value: 5,
          text: '已审核'
        }, {
          value: 6,
          text: '已拒绝'
        }]
      },
      mongoInfo: {
        type:'UPDATE',
        name: 'subject',
        queryParams: {
          unique: ''
        },
        updateParams: {
          reason: ''
        },
        queryColumns: ['abc']
      }
    }
  },
  methods: {
    examSubject(data){
      let row = this.pageResult.content[data];
      this.detail(row.unique);
    },
    agreeDone() {
      this.$confirm('是否确认审核通过?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$api.mongo.agree(this.mongoInfo).then((res)=>{
          if(res.resultCode === 0){
            this.$message.success("审核成功")
            this.findPage();
          }else {
            this.$message.error(res.msg);
          }
        }).catch((res) => {
          this.$message.error(res.msg);
        });
      }).catch((res) => {
        this.$message.error(res.msg);
      });
    },
    refuseDone() {
      this.$confirm('是否确认审核拒绝?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$api.mongo.disagree(this.mongoInfo).then((res)=>{
          if(res.resultCode === 0){
            this.$message.success("审核成功")
            this.findPage();
          }else {
            this.$message.error(res.msg);
          }
        });
      }).catch(() => {
      });
    },
    detail(unique){
      this.viewName = 'examInfo';
      if(unique != null){
        this.exam.examUnique = unique;
        this.mongoInfo.queryParams.unique = unique;
      }
      this.$api.article.detail({unique:this.exam.examUnique}).then((res)=>{
        if(res.resultCode === 0){
          if(res.body){
            this.exam.examEdit.title = res.body.title;
            this.exam.examEdit.htmlContent = res.body.content;
            this.exam.examEdit.htmlSubject = res.body.subject;
          }else {
            this.$message.warning("该内容状态已变更，请刷新后重试")
          }
        }
      });
    },
    findPage: function (data) {
      if (data != null) {
        this.pageRequest = data.pageRequest
      }
      this.pageRequest.title = this.filters.name
      this.pageRequest.authorName = this.filters.authorName
      this.pageRequest.type = this.exam.type
      this.pageRequest.status = this.exam.status
      this.$api.article.findPage(this.pageRequest).then((res) => {
        this.pageResult = res.body
      }).then(data != null ? data.callback : '')
    },
    resizeRequest: function(val){
      this.pageRequest.pageSize= val;
      this.findPage();
    },
    refreshPageRequest: function(val){
      this.pageRequest.pageNum = val;
      this.findPage();
    },
    // 处理表格列过滤显示
    initColumns: function () {
      this.columns = [
        {prop: 'id', label: 'ID', width:70,minWidth: 70},
        {prop: 'title', label: '文章标题', minWidth: 260},
        {prop: 'createTime', label: '创建时间', minWidth: 120, formatter: this.dateFormat},
        {prop: 'unique', label: '唯一码', minWidth: 120},
        {prop: 'type', dialogVisible: false, width: 1},
        // {prop:"createBy", label:"创建人", minWidth:120},
        // {prop:"createTime", label:"创建时间", minWidth:120, formatter:this.dateFormat}
        // {prop:"lastUpdateBy", label:"更新人", minWidth:100},
        // {prop:"lastUpdateTime", label:"更新时间", minWidth:120, formatter:this.dateFormat}
      ]
      this.filterColumns = JSON.parse(JSON.stringify(this.columns))
      this.findPage()
    },
    handleEdit: function (data) {
      this.$router.push({
        path: '/content/articleAdd',
        query: {
          id: data.row.unique,
          type: data.row.type
        }
      })
    },
    handleAdd: function (data) {
      this.$router.push('/content/articleAdd')
    },
    handleDelete: function (data) {
      this.$api.article.batchDelete(data.params).then((res) => {
        data.callback(res)
      })
    },
    // 处理表格列过滤显示
    handleFilterColumns: function (data) {
      this.filterColumns = data.filterColumns
      this.$refs.tableColumnFilterDialog.setDialogVisible(false)
    },
    // 处理表格列过滤显示
    displayFilterColumnsDialog: function () {
      this.$refs.tableColumnFilterDialog.setDialogVisible(true)
    },
  },
  computed: {
    prop () {
      let ss = {
        subfield: false,// 单双栏模式
        defaultOpen: 'preview',//edit： 默认展示编辑区域 ， preview： 默认展示预览区域
        editable: false,
        toolbarsFlag: false,
        scrollStyle: true
      }
      return ss
    }
  },
  mounted () {
    this.initColumns()
  }
};
